﻿var selects = {
//绑定数据
bind : function(o,d) {
o['_data'] = d;
for (var i=0; i<d.length; i++) {
if (!d[i].index && !d[i].value && !d[i].text) {
this._addOption(o,d[i].data);
}
}
},
//确定父子关系
parent : function(op,oc){
change = function(){
selects._clearOption(oc);
var d = oc['_data'];
var idx = op.selectedIndex;
var v = op.options[idx].value;
var t = op.options[idx].text;
for (var i=0; i<d.length; i++) {
if ((d[i].value == v) || (d[i].text == t) || (d[i].index == idx)) {
selects._addOption(oc,d[i].data);
}
}
selects._fireEvent(oc,'change');
};
this._addEvent(op,'change',change);
},
//设置默认值
selected : function(o,d) {
for (var i=0; i<o.options.length; i++) {
if ((o.options[i].text == d.text) || (o.options[i].value == d.value) || !!o.options[d.index]) {
o.options[i].selected = true;
};
}
this._fireEvent(o,'change');
},
//数据拆分
_data : {
value : function(d) {
return d.value ? d.value : (d instanceof Array ? d[0] : d);
},
text : function(d) {
return d.text ? d.text : (d instanceof Array && d[1] ? d[1] : this.value(d));
}
},
//添加
_addOption : function(o,d) {
if (d instanceof Array) {
for (var i=0; i<d.length; i++) {
o.options.add(new Option(this._data.text(d[i]),this._data.value(d[i])));
}
} else {
o.options.add(new Option(this._data.text(d),this._data.value(d)));
}
},
//删除索引选项
_removeOption : function(o,i) {
if (o.options.remove) {
o.options.remove(i);
} else {
o.options[i] = null;
}
},
//清空
_clearOption : function(o) {
o.options.length = 0 ;
},
//强制触发
_fireEvent : function(o,n) {
if (o.fireEvent) {
o.fireEvent('on' + n);
} else {
var evt = document.createEvent('HTMLEvents');
evt.initEvent(n,true,true);
o.dispatchEvent(evt);
}
},
//添加事件
_addEvent : function(o, n, f) {
if (o.attachEvent) {
o.attachEvent('on' + n, f);
} else if (o.addEventListener) {
o.addEventListener(n, f, false);
} else {
o['on' + n] = f;
}
},
//删除事件
_removeEvent : function(o, n, f) {
if (o.detachEvent) {
o.detachEvent('on' + n, f);
} else if (o.removeEventListener) {
o.removeEventListener(n, f, false);
} else {
o['on' + n] = null;
}
},
//版权
version : {
author : 'zhaoxunzhiyin',
contact : 'http://hi.baidu.com/zhaoxunzhiyin/',
major : '1',
minor : '0.3',
revision : 'rc',
release : '2014-01-01',
get : function(){
with(this){
return major + '.' + minor + '.' + revision;
};
}
}
};